AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )


include('shared.lua')

local Energy_Increment = 1000
local Energy_Uncharge = 5000
local Refire_Rate = 5

function ENT:SpawnFunction( ply, tr)
	
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 30
	
	local ent = ents.Create( "empcoil" )
		ent:SetPos( SpawnPos )
		ent:SetAngles( ent:GetForward():Angle() )
	ent:Spawn()
	ent:Activate()
	ent:SetVar("Owner", ply)
	return ent

end

function ENT:Initialize()
	self.Entity:SetModel("models/props_combine/combine_mortar01a.mdl")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )      // Make us work with physics,  	
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )   // after all, gmod is a physics  	
	self.Entity:SetSolid( SOLID_VPHYSICS )    
	local phys = self.Entity:GetPhysicsObject()
	if ( RES_DISTRIB == 1 ) then
		RD_AddResource(self.Entity, "energy", 0)
	end
	self.val1 = 0
	self.Active = 0
	self.Ball = 0
	self.Charging = 0
	self.charged = 0
	self.ChargeTime = CurTime()
	self.timer = CurTime() +  1
	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity, { "Fire", "Ball", "Charge" }) end
	if not (WireAddon == nil) then self.Outputs = Wire_CreateOutputs(self.Entity, { "charged" }) end
	self:TriggerInput("Fire", 0)
	self:TriggerInput("Charge", 0)
	self:TriggerInput("Ball", 0)
	if (phys:IsValid()) then
		phys:Wake()
	end
end

function ENT:OnRemove()
	if (RES_DISTRIB == 1) then Dev_Unlink_All(self.Entity) end
end

function ENT:Attack()
if self.Ball == 1 then
	local Pos = self.Entity:GetPos() + (self.Entity:GetUp()*-10)
	local down = self.Entity:GetUp() * -1
	local Ang = down:Angle()
	
	
	local projectile = ents.Create( "empball" )
			
		projectile:SetPos( Pos )
		
		projectile:SetAngles(Ang)
		projectile:Spawn()
		projectile:SetVar("Owner",self.Entity:GetVar("Owner"))
		
		local phys = self.Entity:GetPhysicsObject()
		constraint.NoCollide( projectile, self.Entity, 0, 0 )
		
else
for k,v in pairs(ents.FindInSphere(self.Entity:GetPos(),1500)) do
empattack(self.Entity:GetVar("Owner"),v,30)
end
doempspark(self.Entity:GetPos()+Vector(0,0,50),10)
doempspark(self.Entity:GetPos(),10)
doempspark(self.Entity:GetPos()+Vector(0,50,0),10)
doempspark(self.Entity:GetPos()+Vector(50,0,0),10)
doempspark(self.Entity:GetPos()+Vector(-50,0,0),10)
doempspark(self.Entity:GetPos()+Vector(0,-50,0),10)
doempspark(self.Entity:GetPos()+Vector(0,0,-50),10)
doempspark(self.Entity:GetPos()+Vector(0,0,50),10)


doempspark(self.Entity:GetPos()+Vector(0,300,0),10)
doempspark(self.Entity:GetPos()+Vector(300,0,0),10)
doempspark(self.Entity:GetPos()+Vector(-300,0,0),10)
doempspark(self.Entity:GetPos()+Vector(0,-300,0),10)
doempspark(self.Entity:GetPos()+Vector(0,0,-300),10)

doempspark(self.Entity:GetPos()+Vector(0,500,0),10)
doempspark(self.Entity:GetPos()+Vector(500,0,0),10)
doempspark(self.Entity:GetPos()+Vector(-500,0,0),10)
doempspark(self.Entity:GetPos()+Vector(0,-500,0),10)
doempspark(self.Entity:GetPos()+Vector(0,0,-500),10)

doempspark(self.Entity:GetPos()+Vector(0,800,0),10)
doempspark(self.Entity:GetPos()+Vector(800,0,0),10)
doempspark(self.Entity:GetPos()+Vector(-800,0,0),10)
doempspark(self.Entity:GetPos()+Vector(0,-800,0),10)
doempspark(self.Entity:GetPos()+Vector(0,0,-800),10)

doempspark(self.Entity:GetPos()+Vector(0,1000,0),10)
doempspark(self.Entity:GetPos()+Vector(1000,0,0),10)
doempspark(self.Entity:GetPos()+Vector(-1000,0,0),10)
doempspark(self.Entity:GetPos()+Vector(0,-1000,0),10)
doempspark(self.Entity:GetPos()+Vector(0,0,-1000),10)

doempspark(self.Entity:GetPos()+Vector(0,1500,0),10)
doempspark(self.Entity:GetPos()+Vector(1500,0,0),10)
doempspark(self.Entity:GetPos()+Vector(-1500,0,0),10)
doempspark(self.Entity:GetPos()+Vector(0,-1500,0),10)
doempspark(self.Entity:GetPos()+Vector(0,0,-1500),10)

doempspark(self.Entity:GetPos()+Vector(0,1500,1500),10)
doempspark(self.Entity:GetPos()+Vector(1500,1500,0),10)
doempspark(self.Entity:GetPos()+Vector(1500,0,1500),10)
doempspark(self.Entity:GetPos()+Vector(-1500,-1500,0),10)
doempspark(self.Entity:GetPos()+Vector(0,-1500,0),10)
doempspark(self.Entity:GetPos()+Vector(-1500,0,-1500),10)
doempspark(self.Entity:GetPos()+Vector(0,-1500,-1500),10)
end
end

function ENT:TriggerInput(iname, value)
	if (iname == "Fire") then
		if (value ~= 0) then
			self.Active = 1
		else
			self.Active = 0
		end
	elseif (iname == "Charge") then
		if value ~= 0 then
			if not self.Charging then
						self.ChargeTime = CurTime() + 5
			end
			self.Charging = 1
		else
			self.Charging = 0
			self.charged= 0
		end
	elseif (iname == "Ball") then
		if value ~= 0 then
			self.Ball = 1
		else
			self.Ball = 0
		end
	end
end



function ENT:Think()
local energy = RD_GetResourceAmount(self.Entity, "energy")

	if self.Active == 1 and self.charged == 1 then
	if energy > Energy_Uncharge then
	RD_ConsumeResource(self.Entity, "energy", Energy_Uncharge)
	self:Attack()
	self.Active = 0
	self.charged = 0
	self.ChargeTime = CurTime() + 5
	end
	end
	
	if ( self.timer > CurTime() ) then return end
	self.timer = CurTime() +  1
	
	if self.Charging == 1 and self.charged == 0 then
	if energy < Energy_Increment then
	self.charged = 0
	self.Charging = 0
	return end
	RD_ConsumeResource(self.Entity, "energy", Energy_Increment)
	elseif self.Charging == 1 and self.charged == 1 then
	if energy < Energy_Increment/2 then
	self.charged = 0
	self.Charging = 0
	return end
	RD_ConsumeResource(self.Entity, "energy", Energy_Increment/2)
	end
	
		
	if self.Charging == 1 then
	doempspark(self.Entity:GetPos()+(self.Entity:GetUp()*-9.5),2)
	if	self.ChargeTime < CurTime() then
	self.charged = 1
	end
	end
				
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "charged", self.charged) end
	
end

function ENT:PreEntityCopy()
	if (RES_DISTRIB == 1) then RD_BuildDupeInfo(self.Entity) end
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	if (RES_DISTRIB == 1) then RD_ApplyDupeInfo(Ent, CreatedEntities) end
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
